﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace blackhouse.Web.FasterFiles
{
    public class CssCompressor :ICompressOutput
    {
        
        public string CompressOutput(string css)
        {
            css = css.Replace("  ", String.Empty);
            css = css.Replace(Environment.NewLine, String.Empty);
            css = css.Replace("\t", string.Empty);
            css = css.Replace(" {", "{");
            css = css.Replace(" :", ":");
            css = css.Replace(": ", ":");
            css = css.Replace(", ", ",");
            css = css.Replace("; ", ";");
            css = css.Replace(";}", "}");

            // sometimes found when retrieving CSS remotely
            css = css.Replace(@"?", string.Empty);

            //body = Regex.Replace(body, @"/\*[^\*]*\*+([^/\*]*\*+)*/", "$1");
            css = Regex.Replace(css, @"(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,}(?=&nbsp;)|(?<=&ndsp;)\s{2,}(?=[<])", String.Empty);

            //Remove comments from CSS
            css = Regex.Replace(css, @"/\*[\d\D]*?\*/", string.Empty);

            return css;
        }


        ///// <summary>
        ///// This will make the browser and server keep the output
        ///// in its cache and thereby improve performance.
        ///// </summary>
        //private static void SetHeaders(int hash, HttpContext context)
        //{
        //    context.Response.ContentType = "text/css";
        //    context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

        //    context.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().AddDays(7));
        //    context.Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
        //    context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

        //    string etag = "\"" + hash.ToString() + "\"";
        //    string incomingEtag = context.Request.Headers["If-None-Match"];

        //    context.Response.Cache.SetETag(etag);

        //    if (String.Compare(incomingEtag, etag) == 0)
        //    {
        //        context.Response.Clear();
        //        context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotModified;
        //        context.Response.SuppressContent = true;
        //    }
        //}

        //#region Compression

        //private const string GZIP = "gzip";
        //private const string DEFLATE = "deflate";

        //private static void Compress(HttpContext context)
        //{
        //    if (context.Request.UserAgent != null && context.Request.UserAgent.Contains("MSIE 6"))
        //        return;

        //    if (IsEncodingAccepted(GZIP))
        //    {
        //        context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
        //        SetEncoding(GZIP);
        //    }
        //    else if (IsEncodingAccepted(DEFLATE))
        //    {
        //        context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
        //        SetEncoding(DEFLATE);
        //    }
        //}

        ///// <summary>
        ///// Checks the request headers to see if the specified
        ///// encoding is accepted by the client.
        ///// </summary>
        //private static bool IsEncodingAccepted(string encoding)
        //{
        //    return HttpContext.Current.Request.Headers["Accept-encoding"] != null && HttpContext.Current.Request.Headers["Accept-encoding"].Contains(encoding);
        //}

        ///// <summary>
        ///// Adds the specified encoding to the response headers.
        ///// </summary>
        ///// <param name="encoding"></param>
        //private static void SetEncoding(string encoding)
        //{
        //    HttpContext.Current.Response.AppendHeader("Content-encoding", encoding);
        //}

        //#endregion

    }
}